import QtQuick

MouseArea {
    id: resizeArea

    required property var item

    anchors.fill: parent
    hoverEnabled: true
    cursorShape: Qt.ArrowCursor
    smooth: true

    property int minWidth: 800
    property int minHeight: 600

    property int edgeSize: 10

    property bool isLeftEdge: false
    property bool isRightEdge: false
    property bool isTopEdge: false
    property bool isBottomEdge: false
    property bool isLeftTopCorner: false
    property bool isRightTopCorner: false
    property bool isLeftBottomCorner: false
    property bool isRightBottomCorner: false
    property bool isInsideWindow: false

    property real startMouseX: 0
    property real startMouseY: 0
    property real startWindowX: 0
    property real startWindowY: 0
    property real startWidth: 0
    property real startHeight: 0

    property bool isResizing: false

    onPositionChanged: function (mouse) {
        if (resizeArea.isResizing === false) {
            const x = mouse.x;
            const y = mouse.y;
            const w = resizeArea.width;
            const h = resizeArea.height;

            // 重置状态
            resetEdgeStates();

            // 检测边缘和角落
            resizeArea.isLeftEdge = x < resizeArea.edgeSize;
            resizeArea.isRightEdge = x > w - resizeArea.edgeSize;
            resizeArea.isTopEdge = y < resizeArea.edgeSize;
            resizeArea.isBottomEdge = y > h - resizeArea.edgeSize;

            // 检测角落
            resizeArea.isLeftTopCorner = resizeArea.isLeftEdge && resizeArea.isTopEdge;
            resizeArea.isRightTopCorner = resizeArea.isRightEdge && resizeArea.isTopEdge;
            resizeArea.isLeftBottomCorner = resizeArea.isLeftEdge && resizeArea.isBottomEdge;
            resizeArea.isRightBottomCorner = resizeArea.isRightEdge && resizeArea.isBottomEdge;

            // 设置鼠标样式
            updateCursorShape();
        }

        if (resizeArea.pressed) {
            handleResize(mouse);
        }
    }

    onPressed: function (mouse) {
        resizeArea.startMouseX = mouse.x + item.x;
        resizeArea.startMouseY = mouse.y + item.y;
        resizeArea.startWindowX = item.x;
        resizeArea.startWindowY = item.y;
        resizeArea.startWidth = item.width;
        resizeArea.startHeight = item.height;

        if (cursorShape !== Qt.ArrowCursor) {
            resizeArea.isResizing = true;
        }
    }

    onReleased: function (mouse) {
        resizeArea.isResizing = false;
    }

    function resetEdgeStates() {
        resizeArea.isLeftEdge = false;
        resizeArea.isRightEdge = false;
        resizeArea.isTopEdge = false;
        resizeArea.isBottomEdge = false;
        resizeArea.isLeftTopCorner = false;
        resizeArea.isRightTopCorner = false;
        resizeArea.isLeftBottomCorner = false;
        resizeArea.isRightBottomCorner = false;
    }

    function updateCursorShape() {
        if (resizeArea.isLeftTopCorner || resizeArea.isRightBottomCorner) {
            resizeArea.cursorShape = Qt.SizeFDiagCursor;
        } else if (resizeArea.isRightTopCorner || resizeArea.isLeftBottomCorner) {
            resizeArea.cursorShape = Qt.SizeBDiagCursor;
        } else if (resizeArea.isLeftEdge || resizeArea.isRightEdge) {
            resizeArea.cursorShape = Qt.SizeHorCursor;
        } else if (resizeArea.isTopEdge || resizeArea.isBottomEdge) {
            resizeArea.cursorShape = Qt.SizeVerCursor;
        } else {
            resizeArea.cursorShape = Qt.ArrowCursor;
        }
    }

    function handleResize(mouse) {
        const deltaX = item.x + mouse.x - resizeArea.startMouseX;
        const deltaY = item.y + mouse.y - resizeArea.startMouseY;

        if (resizeArea.isLeftTopCorner) {
            item.x = resizeArea.startWindowX + deltaX;
            item.width = resizeArea.startWidth - deltaX > resizeArea.minWidth ? resizeArea.startWidth - deltaX : resizeArea.minWidth;
            item.y = resizeArea.startWindowY + deltaY;
            item.height = resizeArea.startHeight - deltaY > resizeArea.minHeight ? resizeArea.startHeight - deltaY : resizeArea.minHeight;
        } else if (resizeArea.isRightTopCorner) {
            item.width = resizeArea.startWidth + deltaX > resizeArea.minWidth ? resizeArea.startWidth + deltaX : resizeArea.minWidth;
            item.y = resizeArea.startWindowY + deltaY;
            item.height = resizeArea.startHeight - deltaY > resizeArea.minHeight ? resizeArea.startHeight - deltaY : resizeArea.minHeight;
        } else if (resizeArea.isLeftBottomCorner) {
            item.x = resizeArea.startWindowX + deltaX;
            item.width = resizeArea.startWidth - deltaX > resizeArea.minWidth ? resizeArea.startWidth - deltaX : resizeArea.minWidth;
            item.height = resizeArea.startHeight + deltaY > resizeArea.minHeight ? resizeArea.startHeight + deltaY : resizeArea.minHeight;
        } else if (resizeArea.isRightBottomCorner) {
            item.width = resizeArea.startWidth + deltaX > resizeArea.minWidth ? resizeArea.startWidth + deltaX : resizeArea.minWidth;
            item.height = resizeArea.startHeight + deltaY > resizeArea.minHeight ? resizeArea.startHeight + deltaY : resizeArea.minHeight;
        } else if (resizeArea.isLeftEdge) {
            item.x = resizeArea.startWindowX + deltaX;
            item.width = resizeArea.startWidth - deltaX > resizeArea.minWidth ? resizeArea.startWidth - deltaX : resizeArea.minWidth;
        } else if (resizeArea.isRightEdge) {
            item.width = resizeArea.startWidth + deltaX > resizeArea.minWidth ? resizeArea.startWidth + deltaX : resizeArea.minWidth;
        } else if (resizeArea.isTopEdge) {
            item.y = resizeArea.startWindowY + deltaY;
            item.height = resizeArea.startHeight - deltaY > resizeArea.minHeight ? resizeArea.startHeight - deltaY : resizeArea.minHeight;
        } else if (resizeArea.isBottomEdge) {
            item.height = resizeArea.startHeight + deltaY > resizeArea.minHeight ? resizeArea.startHeight + deltaY : resizeArea.minHeight;
        }
    }
}